Date.prototype.format = function (fmt) { //author: meizz
  var o:any = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
          .substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k] ).length)));
  return fmt;
}
Array.prototype.last = function(){
  return this.length==0?null:this[this.length-1];
}
//深度克隆
export function deepClone(obj:{[key:string]:any}){
  let result,oClass=isClass(obj);
      //确定result的类型
  if(oClass==="Object"){
      result={};
  }else if(oClass==="Array"){
      result=[];
  }else{
      return obj;
  }
  for(let key in obj){
      var copy=obj[key];
      if(isClass(copy)=="Object"){
        (<{[key:string]:any}>result)[key]=deepClone(copy);//递归调用
      }else if(isClass(copy)=="Array"){
        (<{[key:string]:any}>result)[key]=deepClone(copy);
      }else{
        (<{[key:string]:any}>result)[key]=obj[key];
      }
  }
  return result;
}
//返回传递给他的任意对象的类
function isClass(o:any){
  if(o===null) return "Null";
  if(o===undefined) return "Undefined";
  return Object.prototype.toString.call(o).slice(8,-1);
}

export function sysUuid():string{
  return "__uuid"+window.setTimeout(()=>{},0);
}


export function EmptyFunc(){};

export function getValue(values:{[key:string]:any},path:string){
  let tmp:any = values;
  const keys = path.split('.');
  keys.find(k=>{
    if(/\[\d+\]$/.test(k)){
      let idx = k.split(/[\[\]]/);
      tmp = tmp[idx[0]];
      if(tmp==null||!Array.isArray(tmp)||tmp.length <= parseInt(idx[1]) ){
        tmp = null;
        return true;
      }else{
        tmp = tmp[parseInt(idx[1])];
        return false;
      }
    }

    if(tmp[k]==null){
      tmp = null;
      return true;
    }
    tmp = tmp[k];
    return false;
  })
  return tmp;
}

export function formatMD(d:number|string){
  return `0${d}`.replace(/^\d*(?=\d{2}$)/,'');
}

export function trimNull<T>(v:T|null):T|undefined{
  if(v==null)return undefined;
  if(Array.isArray(v)){
    return v.length==0?undefined:v;
  }
  if(typeof v=='string'){
    return v.trim()==''?undefined:v;
  }
  return undefined;
}